package com.wing.bill.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.bill.dao.WarehouseProcessBillDao;
import com.wing.bill.dao.WarehouseProcessBillDetailDao;
import com.wing.bill.model.entity.WarehouseBill;
import com.wing.bill.model.entity.WarehouseProcessBill;
import com.wing.bill.model.entity.WarehouseProcessBillDetail;
import com.wing.bill.model.enums.BillStatusEnum;
import com.wing.bill.model.response.WarehouseProcessBillDetailVO;
import com.wing.bill.model.response.WarehouseProcessBillVO;
import com.wing.common.utils.DateUtils;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class WarehouseProcessBillServiceImpl extends ServiceImpl<WarehouseProcessBillDao, WarehouseProcessBill> implements WarehouseProcessBillService{

    @Autowired
    WarehouseProcessBillDetailService warehouseProcessBillDetailService;
    @Autowired
    WarehouseProcessBillDetailDao warehouseProcessBillDetailDao;

    @Override
    public Page<WarehouseProcessBillVO> pageList4app(Page pg, Map<String, Object> paramMap) {
        List<WarehouseProcessBillVO> list = baseMapper.list4app(pg, paramMap);
        pg.setRecords(list);
        return pg;
    }

    // 判断用户账单是否存在
    @Override
    public WarehouseProcessBill isExistBill(Long customerId){
        return baseMapper.selectOne(new LambdaQueryWrapper<WarehouseProcessBill>().eq(WarehouseProcessBill::getCustomerId, customerId).eq(WarehouseProcessBill::getBillPeriod, DateUtils.getYearMonth()));
    }
    // 新增本月账单
    @Override
    public WarehouseProcessBill createBill(Long customerId){
        WarehouseProcessBill warehouseProcessBill = new WarehouseProcessBill();
        warehouseProcessBill.setCustomerId(customerId);
        warehouseProcessBill.setBillStatus(BillStatusEnum.UNPAID.getCode());
        warehouseProcessBill.setBillPeriod(DateUtils.getYearMonth());
        warehouseProcessBill.setCode(UniqueIdGeneratorUtil.generateUniqueId("SCZD", "yyyyMMddHHmmss", 1000, 9999));
        baseMapper.insert(warehouseProcessBill);
        return warehouseProcessBill;
    }

    @Override
    public WarehouseProcessBill settlement(Long id , Double amount ,String billVoucherUrl) {
        WarehouseProcessBill bill = baseMapper.selectById(id);
        bill.setBillStatus(1);
        bill.setBillPrice(amount);
        bill.setBillVoucherUrl(billVoucherUrl);
        baseMapper.updateById(bill);
        warehouseProcessBillDetailService.updateBillDetailByBillId(bill.id);
        return bill;
    }

    @Override
    public WarehouseProcessBillVO selectBill(String id){
        WarehouseProcessBillVO warehouseProcessBillVO = this.baseMapper.selectBill(id);

        List<WarehouseProcessBillDetailVO> warehouseBillDetails = warehouseProcessBillDetailDao.selectProcessDetail(id);

//        LambdaQueryWrapper<WarehouseProcessBillDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(WarehouseProcessBillDetail::getWarehouseBillId,id)
//                .eq(WarehouseProcessBillDetail::getIsDeleted,0);
//        List<WarehouseProcessBillDetail> warehouseBillDetails = warehouseProcessBillDetailDao.selectList(lambdaQueryWrapper);
        warehouseProcessBillVO.setWarehouseProcessBillDetailVOList(warehouseBillDetails);
        return warehouseProcessBillVO;
    }
}
